/***************************************************************************
 *   Copyright (C) 2010 by Oleg Goncharov  *
 *   $EMAIL$                           *                          
 *                                                                         *
 *   This file is part of ChessVision.                                     *
 *                                                                         *
 *   ChessVision is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <opencv/cv.h>

#include "cpointnorm.h"

IplImage * CPointNorm::Normolize(const IplImage * src) {
	IplImage * out, * tmp1, *tmp2, *tmp3, *tmp4;
	switch (mode)  {
	case BRIGHTNESS_NORM:
		out = cvCreateImage(cvGetSize(src), IPL_DEPTH_32F, 3);
		tmp1 = cvCreateImage(cvGetSize(src), IPL_DEPTH_32F, 1);
		tmp2 = cvCreateImage(cvGetSize(src), IPL_DEPTH_32F, 1);
		tmp3 = cvCreateImage(cvGetSize(src), IPL_DEPTH_32F, 1);
		tmp4 = cvCreateImage(cvGetSize(src), IPL_DEPTH_32F, 3);
	
	//TODO: Можно ли что нибудь здесь сделаь?
		cvConvert(src, out);
		//cvCvtColor(out, tmp1, CV_BGR2GRAY);
		cvSplit(out, tmp1, tmp2, tmp3, 0);
		cvAdd(tmp1, tmp2, tmp1);
		cvAdd(tmp1, tmp3, tmp1);
		cvMerge(tmp1, tmp1, tmp1, 0, tmp4);
		cvDiv(out, tmp4, out);
	
		cvReleaseImage(&tmp1);
		cvReleaseImage(&tmp2);
		cvReleaseImage(&tmp3);
		cvReleaseImage(&tmp4);
		break;
	case HUE_NORM:
		out = cvCreateImage(cvGetSize(src), IPL_DEPTH_32F, 1);
		tmp1 = cvCreateImage(cvGetSize(src), IPL_DEPTH_32F, 3);
		tmp2 = cvCreateImage(cvGetSize(src), IPL_DEPTH_32F, 3);
		
		cvConvertScale(src, tmp1, 1.0/255.0);
		cvCvtColor(tmp1, tmp2, CV_BGR2HSV);
		cvSplit(tmp2, out, 0 , 0, 0);
		cvConvertScale(out, out, 1.0/360.0);
		
		cvReleaseImage(&tmp1);
		cvReleaseImage(&tmp2);
		break;
	case FILTER_NORM:
		out = cvCreateImage(cvGetSize(src), IPL_DEPTH_32F, 3);
		tmp1 = cvCreateImage(cvGetSize(src), IPL_DEPTH_32F, 3);
		
		cvConvert(src, out);
		cvSmooth(out, tmp1, CV_BLUR, 15, 15);
		cvSub(out, tmp1, out);
		cvConvertScale(out, out, 1.0/512.0, 0.5);
		
		cvReleaseImage(&tmp1);
		break;
	default:
	case NO_NORM:
		out = cvCloneImage(src);
		break;
	}
	return out;
}

